package com.lichuang.dedao.curriculum.controller;


import com.lichuang.dedao.curriculum.entity.Curriculum;
import com.lichuang.dedao.curriculum.entity.CurriculumChapter;
import com.lichuang.dedao.curriculum.entity.CurriculumSection;
import com.lichuang.dedao.curriculum.entity.Teacher;
import com.lichuang.dedao.curriculum.mapper.CurriculumMapper;
import com.lichuang.dedao.curriculum.service.impl.CurriculumChapterServiceImpl;
import com.lichuang.dedao.curriculum.service.impl.CurriculumSectionServiceImpl;
import com.lichuang.dedao.curriculum.service.impl.CurriculumServiceImpl;
import com.lichuang.dedao.curriculum.service.impl.TeacherServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaosong
 * @since 2019-07-15
 */
@RestController
@RequestMapping("/curriculum")
public class CurriculumController {

    @Autowired
    private CurriculumServiceImpl curriculumService;

    @Autowired
    private CurriculumChapterServiceImpl curriculumChapter;

    @Autowired
    private CurriculumSectionServiceImpl curriculumSection;

    @Autowired
    private TeacherServiceImpl teacherService;

    @RequestMapping("/getAllCurriculum")
    public List<Curriculum> getAllCurriculum(@RequestParam(value = "map",required = false)Map<String,Object> map){
        if(map == null){
            return curriculumService.list();
        }
        return (List<Curriculum>)curriculumService.listByMap(map);
    }

    @RequestMapping("getCurriculumById")
    public Curriculum getCurriculumById(@RequestParam("id")Integer id){
        Curriculum curriculum = curriculumService.getById(id);
        List<CurriculumChapter> curriculumChapters = curriculumChapter.getChapterByCurriculumId(curriculum.getCurriculumId());
        List<CurriculumSection> curriculumSections = new ArrayList<>();
        for (CurriculumChapter chapter : curriculumChapters) {
            curriculumSections = curriculumSection.getSectionByChapterId(chapter.getId());
            chapter.setCurriculumSections(curriculumSections);
        }
        curriculum.setAlreadyCount(curriculumSections.size());
        curriculum.setCurriculumChapters(curriculumChapters);
        return curriculum;
    }

    @RequestMapping("/updateCurriculumStatusById")
    public Integer updateCurriculumStatusById(@RequestParam("id")Integer id,@RequestParam("status")Integer status){
        return curriculumService.updateCurriculumStatusById(id,status);
    }

    @RequestMapping("/getNewestCurriculum")
    public List<Curriculum> getNewestCurriculum(){
        List<Curriculum> curriculum = curriculumService.getNewestCurriculum();
        for (Curriculum i : curriculum) {
            i.setTeacher(teacherService.getTeacherById(i.getTeacherId()));
        }
       return curriculum;
    }

}